package com.spzx.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("categoryBrand")
@Tag(name = "分类品牌管理模块")
public class CategoryBrandController extends BaseController {
    @Resource
    private CategoryBrandService categoryBrandService;

    @Operation(summary = "新增商品：根据三级分类id加载分类下的品牌列表")
    @GetMapping("listBrandByC3Id/{c3id}")
    public AjaxResult listBrandByC3Id(@PathVariable("c3id")Long c3id){
        List<Brand> brands = categoryBrandService.selectBrandsByC3id(c3id);
        return success(brands);
    }
     /*
        根据分类id和品牌id 分页查询分类品牌列表：
            ?categoryId=3&brandId=1&pageNum=1&pageSize=3
         后端响应数据：需要分类品牌表联查 分类表和品牌表
                查询的数据使用扩展后的CategoryBrand封装
         多表联查，一般自定义sql
     */
     @Operation(summary = "条件查询分类品牌分页数据")
     @GetMapping("list")
     public TableDataInfo list(CategoryBrand categoryBrand){
         Page<CategoryBrand> page = categoryBrandService.listByCidAndBid(categoryBrand);
         return new TableDataInfo(page.getRecords(),(int)page.getTotal());
     }

    @Operation(summary = "查询分类品牌详情")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable("id")Long id){
        CategoryBrand categoryBrand = categoryBrandService.getCategoryBrandById(id);
        return success(categoryBrand);
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping
    public AjaxResult save(@Validated @RequestBody CategoryBrand categoryBrand){
        //1、检查 品牌分类数据是否已存在
        if(!categoryBrandService.checkCategoryBrandIsExists(categoryBrand)){
            return error("新增分类品牌失败，分类品牌已存在");
        }
        //2、保存
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.save(categoryBrand));
    }

    @Operation(summary = "更新分类品牌")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CategoryBrand categoryBrand){
        //1、检查 品牌分类数据是否已存在
        if(!categoryBrandService.checkCategoryBrandIsExists(categoryBrand)){
            return error("更新分类品牌失败，分类品牌已存在");
        }
        //2、更新
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.updateById(categoryBrand));
    }
    /*
        中间表的数据 可以考虑物理删除
            中间表数据一般较大

     */
    @Operation(summary = "批量删除分类品牌")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable("ids") List<Long> ids){

        return toAjax(categoryBrandService.deleteByIds(ids));
    }

}
